package com.mz.ozy.activity.me;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.mz.ozy.R;
import com.mz.ozy.activity.MainActivity;
import com.mz.ozy.activity.login.PhoneSmsLoginActivity;
import com.mz.ozy.adapter.MeItemAdapter;
import com.mz.ozy.base.BaseActivity;
import com.mz.ozy.bean.AccountBean;
import com.mz.ozy.bean.MeBean;
import com.mz.ozy.bean.MeItemBean;
import com.mz.ozy.bean.NewCheckJoinMemberBean;
import com.mz.ozy.bean.NewResponeBean;
import com.mz.ozy.bean.UnreadMsgBean;
import com.mz.ozy.constants.Constants;
import com.mz.ozy.mvp.presenter.MePresenter;
import com.mz.ozy.mvp.view.MeContract;
import com.mz.ozy.utils.SPUtils;
import com.mz.ozy.utils.Utils;
import com.mz.ozy.utils.glideUtils.GlideUtils;
import com.mz.ozy.widget.dialog.BaseDialog;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MeActivity extends BaseActivity<MeContract.View, MePresenter> implements MeContract.View {

    @BindView(R.id.userIconIv)
    ImageView userIconIv;

    @BindView(R.id.tv_username)
    TextView tvUsername;
    @BindView(R.id.tv_phone)
    TextView tvPhone;
    @BindView(R.id.iv_me_right)
    TextView ivMeRight;

    @BindView(R.id.pay_carNumberTv)
    TextView tvPayNum;
    @BindView(R.id.ship_carNumberTv)
    TextView tvShipNum;
    @BindView(R.id.receipt_carNumberTv)
    TextView tvReceiptNum;
    @BindView(R.id.refund_carNumberTv)
    TextView tvRefundNum;
    @BindView(R.id.tv_money)
    TextView tvMoney;
    @BindView(R.id.tv_score)
    TextView tvScore;
    @BindView(R.id.tv_bv)
    TextView tvBv;
    @BindView(R.id.gridview)
    RecyclerView mGridview;
    @BindView(R.id.obIv)
    ImageView obIv;
    @BindView(R.id.image_ordermsg)
    ImageView mImageOrdermsg;
    private MeItemAdapter mSearchAdapter;
    private boolean isFirst;
    private AccountBean dataItem;
    MeBean bean;
    private List<MeItemBean> mBeanList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_me);
        ButterKnife.bind(this);

        initMeItemView();
    }

    @Override
    public void onBackPressed() {
        MainActivity.tabHost.setCurrentTab(0);
    }

    private void initMeItemView() {
        addFixedItem();
        mGridview.setLayoutManager(new GridLayoutManager(this, 4));
        mSearchAdapter = new MeItemAdapter(mBeanList);
        mGridview.setAdapter(mSearchAdapter);
        mSearchAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                if (!Constants.isLogin) {
                    return;
                }
                final MeItemBean item = mSearchAdapter.getItem(position);
                System.out.println("=============="+item.getPostion());
                switch (item.getPostion()) {
                    case 0:
                        if (!Constants.isVip) {
                            showVipDialog();
                            return;
                        }
                        MyBonusDetailActivity.go(MeActivity.this, 0);
                        break;
                    case 1:
                        if (!Constants.isVip) {
                            showVipDialog();
                            return;
                        }
                        startActivity(new Intent(MeActivity.this, MyExtendActivity.class));
                        break;
                    case 2:
                        AddressActivity.go(MeActivity.this, 0);
                        break;
                    case 3:
                        startActivity(new Intent(MeActivity.this, MyCardActivity.class));
                        break;
                    case 4:
                        startActivity(new Intent(MeActivity.this, MyKeepActivity.class));
                        break;
                    case 5:
//                        if (!Constants.isVip) {
//                            showVipDialog();
//                            return;
//                        }
//                        if (dataItem.getShiming() == 1) {
                            Intent intent1 = new Intent(MeActivity.this, InviteCodeActivity.class);
                            intent1.putExtra("userlink", bean.getUserLink());
                            intent1.putExtra("code", bean.getInvitecode());
                            startActivity(intent1);
//                        } else {
//                            final BaseDialog dialog = new BaseDialog(MeActivity.this, R.layout.dialog_private, Gravity.CENTER, true);
//                            ((TextView) dialog.getView(R.id.tv_dialog_content)).setText("请先实名认证");
//                            ((TextView) dialog.getView(R.id.tv_dialog_title)).setText("实名认证");
//                            ((TextView) dialog.getView(R.id.btn_confirm)).setText("去实名");
//                            dialog.getView(R.id.btn_cancel).setOnClickListener(new View.OnClickListener() {
//                                @Override
//                                public void onClick(View v) {
//                                    dialog.toggleDialog();
//                                }
//                            });
//                            dialog.getView(R.id.btn_confirm).setOnClickListener(new View.OnClickListener() {
//                                @Override
//                                public void onClick(View v) {
//                                    VerifiedActivity.go(MeActivity.this, "", "", 0);
//                                    dialog.toggleDialog();
//                                }
//                            });
//                            dialog.toggleDialog();
//                        }
                        break;
                    case 6:
                        startActivity(new Intent(MeActivity.this, AboutMeActivity.class));
                        break;
                    case 7:
                        if (!Constants.isVip) {
                            showVipDialog();
                            return;
                        }
                        GQActivity.go(MeActivity.this, bean.getEquity());
                        break;
                    case 8:
                        startActivity(new Intent(MeActivity.this, ServiceOutletsActivity.class));
                        break;
                    case 9:
                        startActivity(new Intent(MeActivity.this, DistributorActivity.class));
                        break;
                    case 10:
                        Intent intent2 = new Intent(MeActivity.this, SettingActivity.class);
                        intent2.putExtra("phone", bean.getUsername());
                        intent2.putExtra("url", bean.getUseravatar());
                        intent2.putExtra("token", bean.getUpToken());
                        startActivity(intent2);
                        break;
                    case 11:
                        final Intent intent = new Intent(MeActivity.this, BusinessCardActivity.class);
                        intent.putExtra("shiming", dataItem.getShiming());
                        startActivity(intent);
                        break;
                }
            }
        });
    }

    @OnClick({R.id.image_msg, R.id.ll_score, R.id.ll_bv, R.id.ll_balance, R.id.userIconIv, R.id.iv_me_right, R.id.image_set, R.id.inviteQrCodeView
            , R.id.ll_wait_pay, R.id.ll_wait_ship, R.id.ll_wait_receipt, R.id.ll_refund
            , R.id.tv_all_order, R.id.tv_username, R.id.obIv})
    public void onViewClicked(View view) {

        if (view.getId() == R.id.tv_username) {
            if (!Constants.isLogin) {
                startActivity(new Intent(this, PhoneSmsLoginActivity.class));
            }
        }

        if (bean != null && view.getId() == R.id.image_set) {
            Intent intent = new Intent(this, SettingActivity.class);
            intent.putExtra("phone", bean.getUsername());
            intent.putExtra("url", bean.getUseravatar());
            intent.putExtra("token", bean.getUpToken());
            startActivity(intent);
//            Intent intent = new Intent(this, MyAccountActivity.class);
//            intent.putExtra("balance", bean.getUser_balance());
//            intent.putExtra("integral", bean.getUser_integral());
//            startActivity(intent);
            return;
        }

        if(view.getId() == R.id.obIv) {
            if (Constants.isVip) {
                final BaseDialog dialog = new BaseDialog(MeActivity.this, R.layout.dialog_private, Gravity.CENTER, true);
                ((TextView) dialog.getView(R.id.tv_dialog_content)).setText("您已经购买会员啦");
                ((TextView) dialog.getView(R.id.tv_dialog_title)).setText("提示");
                ((TextView) dialog.getView(R.id.btn_confirm)).setText("确定");
                dialog.getView(R.id.btn_cancel).setVisibility(View.GONE);
                dialog.getView(R.id.btn_confirm).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialog.toggleDialog();
                    }
                });
                dialog.toggleDialog();
            } else {
                startActivity(new Intent(this, JoinMemberActivity.class));
            }
            return;
        }

        if (bean != null) {
            switch (view.getId()) {
                case R.id.image_msg:
                    startActivity(new Intent(this, MsgActivity.class));
                    break;
                case R.id.ll_score:
                    //我的兑换券
                    MyScoreActivity.go(MeActivity.this, 0);
                    break;
                case R.id.ll_bv:
                    //我的奖励
                    if (!Constants.isVip) {
                        showVipDialog();
                        return;
                    }
                    startActivity(new Intent(MeActivity.this, MyBonusActivity.class));
                    break;
                case R.id.ll_balance:
                    //我的欧花
                    MybalanceActivity.go(this, Utils.decimalUtils(bean.getRemain(), 2));
                    break;
                case R.id.inviteQrCodeView:
//                    startActivity(new Intent(this, UpVipActivity.class));
                    startActivity(new Intent(this, JoinMemberActivity.class));
                    break;
//                case R.id.userIconIv:
                case R.id.image_set:
//                    Intent intent = new Intent(this, SettingActivity.class);
//                    intent.putExtra("phone", bean.getUsername());
//                    intent.putExtra("url", bean.getUseravatar());
//                    intent.putExtra("token", bean.getUpToken());
//                    startActivity(intent);
                    Intent intent = new Intent(this, MyAccountActivity.class);
                    intent.putExtra("balance", bean.getUser_balance());
                    intent.putExtra("integral", bean.getUser_integral());
                    startActivity(intent);
                    break;
               /* case R.id.msgView:
                    startActivity(new Intent(this, MsgActivity.class));
                    break;
                case R.id.customView:
                    startActivity(new Intent(this, CustomServiceActivity.class));
                    break;*/
               /* case R.id.iv_me_right:
                    startActivity(new Intent(this, SwitchRoleActivity.class));
                    break;*/
                case R.id.ll_wait_pay:  //待付款
                    MyOrderActivity.start(this, MyOrderFragment.TYPE_PAY);
                    break;
                case R.id.ll_wait_ship:  //待发货
                    MyOrderActivity.start(this, MyOrderFragment.TYPE_SEND);
                    break;
                case R.id.ll_wait_receipt:  //待收货
                    MyOrderActivity.start(this, MyOrderFragment.TYPE_RECEIVE);
                    break;
                case R.id.ll_refund:  //待退款
                    ReturnGoodsListActivity.start(this);
                    break;
                case R.id.tv_all_order:  //我的订单
                    MyOrderActivity.start(this, MyOrderFragment.TYPE_ALL);
                    break;

                default:
                    break;
            }
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (Constants.isLogin) {
//            final String username = SPUtils.getInstance(this).getString(Constants.SPName.USERNAME, "");
//            tvUsername.setText(username);
            mPresenter.getUnreadMsg(this);
            mPresenter.getMeInfo(this, isFirst);
            mPresenter.upAccountHeader(this);
            isFirst = false;
        } else {
            tvUsername.setText("登录/注册");
            tvPhone.setVisibility(View.GONE);
        }
    }

    @Override
    public void getgetMeInfoSuccess(MeBean bean) {
        this.bean = bean;
        SPUtils.getInstance(this).putInt("paynum", bean.getPaynum());
        tvMoney.setText(Utils.decimalUtils(bean.getUser_balance(), 2));
        tvScore.setText(Utils.decimalUtils(bean.getUser_integral(), 2));
        tvBv.setText(Utils.decimalUtils(bean.getRemain(), 2));
        tvUsername.setText(TextUtils.isEmpty(bean.getNickname()) ? bean.getUsername() : bean.getNickname());
        SPUtils.getInstance(this).putString(Constants.SPName.USER, bean.getUsername());
        SPUtils.getInstance(this).putString("invite", bean.getInvitecode());
        SPUtils.getInstance(this).putString(Constants.SPName.USERNAME, bean.getUsername());
        SPUtils.getInstance(this).putString(Constants.SPName.USERAVATAR, bean.getUseravatar());
        SPUtils.getInstance(this).putString(Constants.SPName.QINIU_TOKEN, bean.getUpToken());
        userIconIv.setTag(bean.getUseravatar());
        GlideUtils.loadAvatar(this, userIconIv, bean.getUseravatar());
//        GlideUtils.loadRoundImg(this, obIv, bean.getBannerMap().getImgPath(), 7);

        tvPhone.setText("用户ID: " + bean.getInvitecode());
        switch (bean.getGrade()) {
            case 0:
                ivMeRight.setText("普通会员");
                break;
            case 1:
                ivMeRight.setText("VIP");
                break;
            case 2:
                ivMeRight.setText("金卡VIP");
                break;
            case 3:
                ivMeRight.setText("钻卡VIP");
                break;
            case 4:
                ivMeRight.setText("健康顾问");
                break;
            case 5:
                ivMeRight.setText("资深顾问");
                break;
            case 6:
                ivMeRight.setText("蓝宝顾问");
                break;
            case 7:
                ivMeRight.setText("绿宝顾问");
                break;
            case 8:
                ivMeRight.setText("红宝督导");
                break;
            case 9:
                ivMeRight.setText("蓝钻督导");
                break;
            case 10:
                ivMeRight.setText("绿钻督导");
                break;
            case 11:
                ivMeRight.setText("红钻督导");
                break;
            case 12:
                ivMeRight.setText("黑钻首席");
                break;
        }

        if (bean.getDzf() == 0) {
            tvPayNum.setVisibility(View.GONE);
        } else {
            tvPayNum.setVisibility(View.VISIBLE);
            tvPayNum.setText(bean.getDzf() + "");
        }
        if (bean.getDfh() == 0) {
            tvShipNum.setVisibility(View.GONE);
        } else {
            tvShipNum.setVisibility(View.VISIBLE);
            tvShipNum.setText(bean.getDfh() + "");
        }
        if (bean.getDsh() == 0) {
            tvReceiptNum.setVisibility(View.GONE);
        } else {
            tvReceiptNum.setVisibility(View.VISIBLE);
            tvReceiptNum.setText(bean.getDsh() + "");
        }
        if (bean.getDtk() == 0) {
            tvRefundNum.setVisibility(View.GONE);
        } else {
            tvRefundNum.setVisibility(View.VISIBLE);
            tvRefundNum.setText(bean.getDtk() + "");
        }

        boolean hasBonus = true;
        boolean hasServiceOutlets = true;
        boolean hasDistributor = true;
        if (bean.getGrade() == 0) {
            hasBonus = false;
        }
        if (bean.getServies_dot() == 0) {
            hasServiceOutlets = false;
        }
        if (bean.getDistributor() == 0) {
            hasDistributor = false;
        }

        //是否是年卡会员
        if (bean.getGrade() == 0) {
            SPUtils.getInstance(this).putString(Constants.PREE.IS_VIP, "0");
            Constants.isVip = false;
        } else {
            SPUtils.getInstance(this).putString(Constants.PREE.IS_VIP, "1");
            Constants.isVip = true;
        }
//        addMeItem(hasBonus, hasServiceOutlets, hasDistributor);
    }

    private boolean mHasBonus = false;
    private boolean mHasServiceOutlets = false;
    private boolean mHasDistributor = false;

    private void addMeItem(boolean hasBonus, boolean hasServiceOutlets, boolean hasDistributor) {

        if (!mHasBonus) {
            if (hasBonus) {
                mHasBonus = true;
//                mBeanList.add(0, new MeItemBean(0, "我的奖励", R.mipmap.me_jj));
                mBeanList.add(3, new MeItemBean(0, "资金明细", R.mipmap.me_jj));
            }
        }
        if (!mHasServiceOutlets) {
            if (hasServiceOutlets) {
                mHasServiceOutlets = true;
                mBeanList.add(new MeItemBean(8, "服务网点", R.mipmap.me_sevice_port));
            }
        }
        if (!mHasDistributor) {
            if (hasDistributor) {
                mHasDistributor = true;
                mBeanList.add(new MeItemBean(9, "经销商", R.mipmap.me_distrobute));
            }
        }
        mSearchAdapter.notifyDataSetChanged();
    }

    private void addFixedItem() {
//        mBeanList.add(new MeItemBean(10, "账号设置", R.mipmap.icon_c2c_set));
        //mBeanList.add(new MeItemBean(5, "推荐返利", R.mipmap.me_invite));
        mBeanList.add(new MeItemBean(0, "奖励明细", R.mipmap.me_jj));
        mBeanList.add(new MeItemBean(5, "分享码", R.mipmap.me_invite));
        mBeanList.add(new MeItemBean(1, "分享列表", R.mipmap.me_friend));
//        mBeanList.add(new MeItemBean(11, "我的名片", R.mipmap.me_address));
        mBeanList.add(new MeItemBean(3, "我的卡券", R.mipmap.me_kj));
        mBeanList.add(new MeItemBean(4, "我的收藏", R.mipmap.me_sc));
        mBeanList.add(new MeItemBean(2, "收货地址", R.mipmap.me_address));
        mBeanList.add(new MeItemBean(6, "关于我们", R.mipmap.me_about));
        mBeanList.add(new MeItemBean(7, "GQ", R.mipmap.me_icon));
        //  mBeanList.add(new MeItemBean(7, "OTA通道", R.mipmap.me_pass));
    }

    @Override
    public void getAccountSuccess(AccountBean bean) {
        dataItem = bean;
    }

    @Override
    public void uploadHeaderSuccess(JSONObject o) {
        mPresenter.getMeInfo(this, isFirst);
    }

    @Override
    public void getxySuccess(MeBean bean) {
    }

    @Override
    public void bindInfo(String type, JSONObject bean) {
    }

    @Override
    public void getAliSuccess(MeBean bean) {
    }

    @Override
    public void exitLoginSuccess(JSONObject bean) {
    }

    @Override
    public void onCancelBindSuccess() {
    }

    @Override
    public void getUnreadMsgSuccess(UnreadMsgBean bean) {
        if(bean.getNums()>0) {
            mImageOrdermsg.setVisibility(View.VISIBLE);
        } else {
            mImageOrdermsg.setVisibility(View.GONE);
        }
    }

}
